home *** CD-ROM | disk | FTP | other *** search
/ Isometric Game Programming with DirectX 7.0 / Isometric Game Programming.iso / source / chapter20 / isohex20_3 / isohex20_3.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-20  |  8.1 KB  |  306 lines

  1. /*****************************************************************************
  2. IsoHex20_3.cpp
  3. Ernest S. Pazera
  4. 20OCT2000
  5. Start a WIN32 Application Workspace, add in this file
  6. Requires GDICanvas.h and GDICanvas.cpp
  7. *****************************************************************************/
  8.  
  9. //////////////////////////////////////////////////////////////////////////////
  10. //INCLUDES
  11. //////////////////////////////////////////////////////////////////////////////
  12. #define WIN32_LEAN_AND_MEAN  
  13.  
  14. #include <windows.h>   
  15. #include "GDICanvas.h"
  16.  
  17. //////////////////////////////////////////////////////////////////////////////
  18. //DEFINES
  19. //////////////////////////////////////////////////////////////////////////////
  20. //name for our window class
  21. #define WINDOWCLASS "ISOHEX20"
  22. //title of the application
  23. #define WINDOWTITLE "IsoHex 20-3"
  24.  
  25. //////////////////////////////////////////////////////////////////////////////
  26. //PROTOTYPES
  27. //////////////////////////////////////////////////////////////////////////////
  28. bool Prog_Init();//game data initalizer
  29. void Prog_Loop();//main game loop
  30. void Prog_Done();//game clean up
  31. void ShowMap(HDC hdc);//show sample map
  32.  
  33. //////////////////////////////////////////////////////////////////////////////
  34. //GLOBALS
  35. //////////////////////////////////////////////////////////////////////////////
  36. HINSTANCE hInstMain=NULL;//main application handle
  37. HWND hWndMain=NULL;//handle to our main window
  38.  
  39. //gdi canvases
  40. CGDICanvas gdicTexture;//texture image
  41. CGDICanvas gdicIsoShape;//shape of the iso image
  42. CGDICanvas gdicSource;//tiled texture goes here
  43. CGDICanvas gdicTiles;//tiles go here
  44. //columns and rows
  45. int iColumns;
  46. int iRows;
  47.  
  48. //////////////////////////////////////////////////////////////////////////////
  49. //WINDOWPROC
  50. //////////////////////////////////////////////////////////////////////////////
  51. LRESULT CALLBACK TheWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
  52. {
  53.     //which message did we get?
  54.     switch(uMsg)
  55.     {
  56.     case WM_KEYDOWN:
  57.         {
  58.             return(0);//handled, so return 0
  59.         }break;
  60.     case WM_DESTROY://the window is being destroyed
  61.         {
  62.  
  63.             //tell the application we are quitting
  64.             PostQuitMessage(0);
  65.  
  66.             //handled message, so return 0
  67.             return(0);
  68.  
  69.         }break;
  70.     case WM_PAINT://the window needs repainting
  71.         {
  72.             //a variable needed for painting information
  73.             PAINTSTRUCT ps;
  74.             
  75.             //start painting
  76.             HDC hdc=BeginPaint(hwnd,&ps);
  77.  
  78.             /////////////////////////////
  79.             //painting code would go here
  80.             /////////////////////////////
  81.             ShowMap(hdc);
  82.  
  83.             //end painting
  84.             EndPaint(hwnd,&ps);
  85.  
  86.             //handled message, so return 0
  87.             return(0);
  88.         }break;
  89.     }
  90.  
  91.     //pass along any other message to default message handler
  92.     return(DefWindowProc(hwnd,uMsg,wParam,lParam));
  93. }
  94.  
  95.  
  96. //////////////////////////////////////////////////////////////////////////////
  97. //WINMAIN
  98. //////////////////////////////////////////////////////////////////////////////
  99. int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
  100. {
  101.     //assign instance to global variable
  102.     hInstMain=hInstance;
  103.  
  104.     //create window class
  105.     WNDCLASSEX wcx;
  106.  
  107.     //set the size of the structure
  108.     wcx.cbSize=sizeof(WNDCLASSEX);
  109.  
  110.     //class style
  111.     wcx.style=CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  112.  
  113.     //window procedure
  114.     wcx.lpfnWndProc=TheWindowProc;
  115.  
  116.     //class extra
  117.     wcx.cbClsExtra=0;
  118.  
  119.     //window extra
  120.     wcx.cbWndExtra=0;
  121.  
  122.     //application handle
  123.     wcx.hInstance=hInstMain;
  124.  
  125.     //icon
  126.     wcx.hIcon=LoadIcon(NULL,IDI_APPLICATION);
  127.  
  128.     //cursor
  129.     wcx.hCursor=LoadCursor(NULL,IDC_ARROW);
  130.  
  131.     //background color
  132.     wcx.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
  133.  
  134.     //menu
  135.     wcx.lpszMenuName=NULL;
  136.  
  137.     //class name
  138.     wcx.lpszClassName=WINDOWCLASS;
  139.  
  140.     //small icon
  141.     wcx.hIconSm=NULL;
  142.  
  143.     //register the window class, return 0 if not successful
  144.     if(!RegisterClassEx(&wcx)) return(0);
  145.  
  146.     //create main window
  147.     hWndMain=CreateWindowEx(0,WINDOWCLASS,WINDOWTITLE, WS_BORDER | WS_SYSMENU | WS_VISIBLE,0,0,640,480,NULL,NULL,hInstMain,NULL);
  148.  
  149.     //error check
  150.     if(!hWndMain) return(0);
  151.  
  152.     //if program initialization failed, then return with 0
  153.     if(!Prog_Init()) return(0);
  154.  
  155.     //message structure
  156.     MSG msg;
  157.  
  158.     //message pump
  159.     for(;;)    
  160.     {
  161.         //look for a message
  162.         if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  163.         {
  164.             //there is a message
  165.  
  166.             //check that we arent quitting
  167.             if(msg.message==WM_QUIT) break;
  168.             
  169.             //translate message
  170.             TranslateMessage(&msg);
  171.  
  172.             //dispatch message
  173.             DispatchMessage(&msg);
  174.         }
  175.  
  176.         //run main game loop
  177.         Prog_Loop();
  178.     }
  179.     
  180.     //clean up program data
  181.     Prog_Done();
  182.  
  183.     //return the wparam from the WM_QUIT message
  184.     return(msg.wParam);
  185. }
  186.  
  187. //////////////////////////////////////////////////////////////////////////////
  188. //INITIALIZATION
  189. //////////////////////////////////////////////////////////////////////////////
  190. bool Prog_Init()
  191. {
  192.     //load texture canvas
  193.     gdicTexture.Load(NULL,"texture.bmp");
  194.  
  195.     //load iso shape
  196.     gdicIsoShape.Load(NULL,"IsoShape.bmp");
  197.  
  198.     //grab dc from main window
  199.     HDC hdc=GetDC(hWndMain);
  200.  
  201.     //calculate rows and columns of tiles
  202.     iColumns=gdicTexture.GetWidth()/gdicIsoShape.GetWidth();
  203.     iRows=2*gdicTexture.GetHeight()/gdicIsoShape.GetHeight();
  204.  
  205.     //create tiled texture canvas
  206.     gdicSource.CreateBlank(hdc,gdicTexture.GetWidth()*2,gdicTexture.GetHeight()*2);
  207.  
  208.     //create tile canvas
  209.     gdicTiles.CreateBlank(hdc,gdicIsoShape.GetWidth()*iColumns,gdicIsoShape.GetHeight()*iRows);
  210.  
  211.     //restore dc to main window
  212.     ReleaseDC(hWndMain,hdc);
  213.  
  214.     //create tiled texture image
  215.     //upper left
  216.     BitBlt(gdicSource,0,0,gdicTexture.GetWidth(),gdicTexture.GetWidth(),gdicTexture,0,0,SRCCOPY);
  217.     //upper right
  218.     BitBlt(gdicSource,gdicTexture.GetWidth(),0,gdicTexture.GetWidth(),gdicTexture.GetWidth(),gdicTexture,0,0,SRCCOPY);
  219.     //lower left
  220.     BitBlt(gdicSource,0,gdicTexture.GetHeight(),gdicTexture.GetWidth(),gdicTexture.GetWidth(),gdicTexture,0,0,SRCCOPY);
  221.     //lower right
  222.     BitBlt(gdicSource,gdicTexture.GetWidth(),gdicTexture.GetHeight(),gdicTexture.GetWidth(),gdicTexture.GetWidth(),gdicTexture,0,0,SRCCOPY);
  223.  
  224.     //rip out the tiles
  225.     int x;
  226.     int y;
  227.     int plotx;
  228.     int ploty;
  229.  
  230.     //loop through columns
  231.     for(x=0;x<iColumns;x++)
  232.     {
  233.         //loop through rows
  234.         for(y=0;y<iRows;y++)
  235.         {
  236.             //determine plotx and ploty
  237.             plotx=x*gdicIsoShape.GetWidth()+(y&1)*gdicIsoShape.GetWidth()/2;
  238.             ploty=y*gdicIsoShape.GetHeight()/2;
  239.  
  240.             //bring plotx and ploty within appropriate range
  241.             plotx%=gdicTexture.GetWidth();
  242.             ploty%=gdicTexture.GetHeight();
  243.  
  244.             //check for negatives
  245.             if(plotx<0) plotx+=gdicTexture.GetWidth();
  246.             if(ploty<0) ploty+=gdicTexture.GetHeight();
  247.  
  248.             //grab this part of the source surface, and place it on the tile surface
  249.             BitBlt(gdicTiles,x*gdicIsoShape.GetWidth(),y*gdicIsoShape.GetHeight(),gdicIsoShape.GetWidth(),gdicIsoShape.GetHeight(),gdicSource,plotx,ploty,SRCCOPY);
  250.  
  251.             //modulate with the iso shape
  252.             BitBlt(gdicTiles,x*gdicIsoShape.GetWidth(),y*gdicIsoShape.GetHeight(),gdicIsoShape.GetWidth(),gdicIsoShape.GetHeight(),gdicIsoShape,0,0,SRCAND);
  253.         }
  254.     }
  255.  
  256.     return(true);//return success
  257. }
  258.  
  259. //////////////////////////////////////////////////////////////////////////////
  260. //CLEANUP
  261. //////////////////////////////////////////////////////////////////////////////
  262. void Prog_Done()
  263. {
  264. }
  265.  
  266. //////////////////////////////////////////////////////////////////////////////
  267. //MAIN GAME LOOP
  268. //////////////////////////////////////////////////////////////////////////////
  269. void Prog_Loop()
  270. {
  271. }
  272.  
  273. void ShowMap(HDC hdc)//show sample map
  274. {
  275.     //vars
  276.     int x;
  277.     int y;
  278.     int tilex;
  279.     int tiley;
  280.     int plotx;
  281.     int ploty;
  282.  
  283.     //clear out dc
  284.     RECT rcFill;
  285.     GetClientRect(hWndMain,&rcFill);
  286.  
  287.     //fill rect
  288.     FillRect(hdc,&rcFill,(HBRUSH)GetStockObject(BLACK_BRUSH));
  289.  
  290.     //loops
  291.     for(x=0;x<10;x++)
  292.     {
  293.         for(y=0;y<20;y++)
  294.         {
  295.             //calculate plotted positions
  296.             plotx=x*gdicIsoShape.GetWidth()+(y&1)*gdicIsoShape.GetWidth()/2;
  297.             ploty=y*gdicIsoShape.GetHeight()/2;
  298.             //calculate which tile to use
  299.             tilex=(x%iColumns)*gdicIsoShape.GetWidth();
  300.             tiley=(y%iRows)*gdicIsoShape.GetHeight();
  301.             //blit the tile
  302.             BitBlt(hdc,plotx,ploty,gdicIsoShape.GetWidth(),gdicIsoShape.GetHeight(),gdicTiles,tilex,tiley,SRCPAINT);
  303.         }
  304.     }
  305. }
  306.